package com.dragon.backstage.convert;

import com.dragon.backstage.domain.param.role.RoleParam;
import com.dragon.backstage.domain.param.user.UserAuthRoleParam;
import com.dragon.backstage.domain.vo.role.RoleDetailVo;
import com.dragon.backstage.domain.vo.role.RoleVo;
import com.dragon.interfaces.constant.RoleConstant;
import com.dragon.interfaces.domain.request.role.RoleRequest;
import com.dragon.interfaces.entity.Role;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;

import java.util.List;

/**
 * 角色模块类型转换
 *
 * @author dragon
 * @version 1.0
 * @date 2024/8/7 17:41
 */
public class RoleConvert {

    public static RoleRequest toRoleRequest(RoleParam roleParam) {
        return RoleRequest
            .builder()
            .pageNum(roleParam.getPageNum())
            .pageSize(roleParam.getPageSize())
            .roleName(roleParam.getRoleName())
            .roleKey(roleParam.getRoleKey())
            .status(roleParam.getStatus())
            .beginTime(roleParam.getBeginTime())
            .endTime(roleParam.getEndTime())
            .excludeRoleKeys(roleParam.getExcludeRoleKeys())
            .build();
    }

    public static RoleVo toRoleVo(Role role) {
        return RoleVo
            .builder()
            .isSuperAdmin(RoleConstant.SUPER_ADMIN_ROLE_KEY.equals(role.getRoleKey()))
            .roleId(role.getRoleId())
            .roleName(role.getRoleName())
            .roleKey(role.getRoleKey())
            .roleSort(role.getRoleSort())
            .status(role.getStatus())
            .createBy(role.getCreateBy())
            .createTime(role.getCreateTime())
            .updateBy(role.getUpdateBy())
            .updateTime(role.getUpdateTime())
            .build();
    }

    public static RoleDetailVo toRoleDetailVo(Role role, List<Long> roleMenuIds) {
        return RoleDetailVo
            .builder()
            .isSuperAdmin(RoleConstant.SUPER_ADMIN_ROLE_KEY.equals(role.getRoleKey()))
            .roleId(role.getRoleId())
            .roleName(role.getRoleName())
            .roleKey(role.getRoleKey())
            .roleSort(role.getRoleSort())
            .status(role.getStatus())
            .remark(role.getRemark())
            .menuIds(CollectionUtils.isEmpty(roleMenuIds) ? Lists.newArrayList() : roleMenuIds)
            .createBy(role.getCreateBy())
            .createTime(role.getCreateTime())
            .updateBy(role.getUpdateBy())
            .updateTime(role.getUpdateTime())
            .build();
    }

    public static RoleRequest toRoleRequest(UserAuthRoleParam userAuthRoleParam) {
        return RoleRequest
            .builder()
            .pageNum(userAuthRoleParam.getPageNum())
            .pageSize(userAuthRoleParam.getPageSize())
            .build();
    }

}
